from django.db import models
from elasticsearch_dsl import Date, Nested, Boolean, \
    analyzer, Completion, Keyword, Text, DocType, Object

from elasticsearch_dsl.analysis import CustomAnalyzer as _CustomAnalyzer
from elasticsearch_dsl.connections import connections

connections.create_connection(hosts=['localhost'])

class CustomAnalyzer(_CustomAnalyzer):
    def get_analysis_definition(self):
        return {}

ik_analyzer = CustomAnalyzer('ik_max_word', filter=['lowercase'])

class BusActrssType(DocType):

    actress_names = Text(analyzer='ik_max_word') # 指定解析器
    actress_links = Keyword()
    image_url = Keyword()

    # 搜索建议
    suggest = Completion(analyzer=ik_analyzer)

    # 指定index type
    class Meta:
        index = 'bus'
        doc_type = 'actress'



class BusVideoType(DocType):
    title = Text(analyzer='ik_max_word') # 指定解析器

    url = Keyword()

    cover = Keyword()

    image_url = Keyword()

    code = Text(analyzer='ik_max_word') # 指定解析器

    pub_date = Date()

    v_len = Keyword()

    director = Object(properties={
        'director_name': Keyword(),
        'director_link': Keyword()
    })

    producers = Object(properties={
        'producers_name': Keyword(),
        'producers_link': Keyword()
    })

    publisher = Object(properties={
        'publisher_name': Keyword(),
        'publisher_link': Keyword()
    })

    series = Object(properties={
        'series_name': Keyword(),
        'series_link': Keyword()
    })

    category = Object(properties={
        'category_names': Keyword(),
        'category_links': Keyword()
    })

    summary_small_image = Keyword()
    summary_large_image = Keyword()

    actress = Object(
        properties={
            'actress_name': Keyword(),
            'actress_link': Keyword()
        }
    )

    # 搜索建议
    suggest = Completion(analyzer=ik_analyzer)

    # 指定index type
    class Meta:
        index = 'bus'
        doc_type = 'video'